package com.acme.bankapp.sc.bank;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.ServerSocket;
import java.net.Socket;

import com.acme.bankapp.domain.bank.Bank;
import com.acme.bankapp.domain.bank.Client;
import com.acme.bankapp.exceptions.bank.ClientExistsException;
import com.acme.bankapp.exceptions.bank.NotEnoughFundsException;

import static com.acme.bankapp.service.bank.BankDataLoaderService.createClientFromString;
import static com.acme.bankapp.service.bank.BankService.addClient;
import static com.acme.bankapp.domain.bank.BankApplication.modifyBank;

public class BankServer {

	public BankServer(final String file) {

		try {
			ObjectInputStream in = new ObjectInputStream(new FileInputStream(
					file));
			bank = (Bank) in.readObject();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} finally {
			try {
				in.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}

	}

	Bank bank = null;
	ServerSocket providerSocket;
	Socket connection = null;
	ObjectOutputStream out;
	ObjectInputStream in;
	String[] mess;

	public void startServer() {
		try {
			providerSocket = new ServerSocket(2004, 10);
			connection = providerSocket.accept();
			ObjectInputStream in = new ObjectInputStream(
					connection.getInputStream());
			ObjectOutputStream out = new ObjectOutputStream(
					connection.getOutputStream());
			while (true) {
				try {
					mess = (String[]) in.readObject();
					if (mess[0].equals("1")) {
						try {
							addClient(bank, createClientFromString(mess[2]));
							out.writeObject(bank);
						} catch (ClientExistsException e) {
							e.printStackTrace();
						}
					} else if (mess[0].equals("2")) {
						try {
							modifyBank(bank, Integer.parseInt(mess[1]),
									"deposit", Integer.parseInt(mess[2]),
									Integer.parseInt(mess[3]));
							out.writeObject(bank);
						} catch (NumberFormatException e) {
							e.printStackTrace();
						} catch (NotEnoughFundsException e) {
							e.printStackTrace();
						}

					} else if (mess[0].equals("3")) {
						try {
							modifyBank(bank, Integer.parseInt(mess[1]),
									"withdraw", Integer.parseInt(mess[2]),
									Integer.parseInt(mess[3]));
							out.writeObject(bank);
						} catch (NumberFormatException e) {
							e.printStackTrace();
						} catch (NotEnoughFundsException e) {
							e.printStackTrace();
						}

					} else if (mess[0].equals("bye")) {
						break;
					}

				} catch (ClassNotFoundException e) {
					e.printStackTrace();
				}
			}

		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				in.close();
				out.close();
				providerSocket.close();
			} catch (IOException ioException) {
				ioException.printStackTrace();
			}
		}

	}

}
